/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.control.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.control.dto.RiskControlMeasureDTO;
import com.hopes.bizz.risk.control.entity.RiskControlMeasureEntity;
import com.hopes.bizz.risk.control.service.RiskControlMeasureService;
import com.hopes.bizz.risk.control.vo.RiskDetailExcelVO;
import com.hopes.bizz.risk.control.vo.RiskIdentifyExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 风险管控措施
 *
 * @author pig
 * @date 2023-07-19 16:16:03
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskControlMeasure" )
@Tag(description = "RiskControlMeasure" , name = "风险管控措施管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskControlMeasureController {

    private final RiskControlMeasureService riskControlMeasureService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param riskControlMeasureDTO 风险管控措施
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('risk_control_measure_view')" )
    public R getRiskControlMeasurePage(Page page, RiskControlMeasureDTO riskControlMeasureDTO) {
        return R.ok(riskControlMeasureService.listPage(page, riskControlMeasureDTO));
    }


    /**
     * 分页查询
     * @param page 分页对象
     * @param riskControlMeasureDTO 风险管控措施
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/fillPage" )
    @PreAuthorize("@pms.hasPermission('risk_control_measure_view')" )
    public R getRiskControlWithTaskPage(Page page, RiskControlMeasureDTO riskControlMeasureDTO) {
        return R.ok(riskControlMeasureService.listWithTaskPage(page, riskControlMeasureDTO));
    }

    /**
     * 风险辨识列表
     * @param riskUnitId 风险单元Id
     * @return           返回值
     */
    @Operation(summary = "风险辨识列表" , description = "风险辨识列表" )
    @GetMapping("/listIdentify/{riskUnitId}" )
    @PreAuthorize("@pms.hasPermission('risk_control_measure_view')" )
    public R listIdentify(@PathVariable Long riskUnitId) {
        Assert.notNull(riskUnitId, "风险单元Id不可为空");
        return R.ok(riskControlMeasureService.listIdentify(riskUnitId));
    }

    /**
     * 风险管控清单
     * @param riskControlMeasureDTO 风险管控清单
     * @return           返回值
     */
    @Operation(summary = "风险管控清单" , description = "风险管控清单" )
    @GetMapping("/detailedList" )
    @PreAuthorize("@pms.hasPermission('risk_control_measure_view')" )
    public R listDetailed(RiskControlMeasureDTO riskControlMeasureDTO) {
        return R.ok(riskControlMeasureService.listDetailed(riskControlMeasureDTO));
    }


    /**
     * 通过id查询风险管控措施
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('risk_control_measure_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(riskControlMeasureService.getById(id));
    }

    /**
     * 新增风险管控措施
     * @param riskControlMeasure 风险管控措施
     * @return R
     */
    @Operation(summary = "新增风险管控措施" , description = "新增风险管控措施" )
    @SysLog("新增风险管控措施" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_control_measure_add')" )
    public R save(@RequestBody RiskControlMeasureEntity riskControlMeasure) {
        return R.ok(riskControlMeasureService.save(riskControlMeasure));
    }

    /**
     * 修改风险管控措施
     * @param riskControlMeasure 风险管控措施
     * @return R
     */
    @Operation(summary = "修改风险管控措施" , description = "修改风险管控措施" )
    @SysLog("修改风险管控措施" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('risk_control_measure_edit')" )
    public R updateById(@RequestBody RiskControlMeasureEntity riskControlMeasure) {
        return R.ok(riskControlMeasureService.updateById(riskControlMeasure));
    }

    /**
     * 通过id删除风险管控措施
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除风险管控措施" , description = "通过id删除风险管控措施" )
    @SysLog("通过id删除风险管控措施" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('risk_control_measure_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskControlMeasureService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 风险管控措施 excel 模板
     *
     * @return excel 文件流
     */
    @SneakyThrows
    @GetMapping("/templateIdentify")
    @PreAuthorize("@pms.hasPermission('risk_control_measure_export')")
    public void templateIdentify(HttpServletResponse response) {
        ClassPathResource resource = new ClassPathResource("file/风险辨识管控.xlsx");
        response.setContentType("application/octet-stream; charset=UTF-8");
        IoUtil.copy(resource.getInputStream(), response.getOutputStream());
    }

    /**
     * 风险管控清单 excel 模板
     *
     * @return excel 文件流
     */
    @SneakyThrows
    @GetMapping("/templateDetail")
    @PreAuthorize("@pms.hasPermission('risk_control_measure_export')")
    public void templateDetail(HttpServletResponse response) {
        ClassPathResource resource = new ClassPathResource("file/风险管控清单.xlsx");
        response.setContentType("application/octet-stream; charset=UTF-8");
        IoUtil.copy(resource.getInputStream(), response.getOutputStream());
    }

    /**
     * 导入excel 表格
     * @param riskUnitId 风险单元Id
     * @param riskIdentifyExcelVOList  导入的数据
     * @return ok fail
     */
    @PostMapping("/importIdentify/{riskUnitId}")
    @PreAuthorize("@pms.hasPermission('risk_control_measure_export')")
    public R importIdentifyExcel(@PathVariable Long riskUnitId, @RequestExcel(headRowNumber = 4, ignoreEmptyRow = true) List<RiskIdentifyExcelVO> riskIdentifyExcelVOList, BindingResult bindingResult) {
        try {
            return riskControlMeasureService.importExcelIdentify(riskUnitId, riskIdentifyExcelVOList, bindingResult);
        }catch (Exception e) {
            log.error("风险辨识管控导入异常！", e);
            return R.failed("风险辨识管控导入异常！");
        }
    }

    /**
     * 导入excel 表格
     * @param riskDetailExcelVOList  导入的数据
     * @return ok fail
     */
    @PostMapping("/importDetail")
    @PreAuthorize("@pms.hasPermission('risk_control_measure_export')")
    public R importDetailExcel(@RequestExcel(headRowNumber = 4, ignoreEmptyRow = true)  List<RiskDetailExcelVO> riskDetailExcelVOList, BindingResult bindingResult) {
        try {
            return riskControlMeasureService.importExcelDetail(riskDetailExcelVOList, bindingResult);
        }catch (Exception e) {
            log.error("风险管控清单导入异常！", e);
            return R.failed("风险管控清单导入异常！");
        }
    }


    /**
     * 导出 风险辨识管控 excel 表格
     * @param riskControlMeasureDTO 查询条件
     * @return excel 文件流
     */
    @GetMapping("/exportIdentify")
    @PreAuthorize("@pms.hasPermission('risk_control_measure_export')")
    public void exportIdentify(RiskControlMeasureDTO riskControlMeasureDTO, HttpServletResponse response) {
        try {
            riskControlMeasureService.exportExcelIdentify(riskControlMeasureDTO, response);
        }catch (Exception e) {
            log.error("风险辨识管控导出异常！", e);
        }
    }

    /**
     * 导出 风险管控清单 excel 表格
     * @param riskControlMeasureDTO 查询条件
     * @return excel 文件流
     */
    @GetMapping("/exportDetail")
    @PreAuthorize("@pms.hasPermission('risk_control_measure_export')")
    public void exportDetail(RiskControlMeasureDTO riskControlMeasureDTO, HttpServletResponse response) {
        try {
            riskControlMeasureService.exportExcelDetail(riskControlMeasureDTO, response);
        }catch (Exception e) {
            log.error("风险管控清单导出异常！", e);
        }
    }
}
